/**************************************************************************
 * @doc PROTOCOL
 * @module PROTOCOL.H | Header file for the Protocol Engine
 *
 * Product     :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :   SoftPay Development Team (I_SP2000).
 * <nl>Notes       :
 *
 * @head3 PROTOCOL Functions |
 * @index | PROTOCOL
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 *
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form
 * or by any means whether electronic, mechanical, magnetic, optical,
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
 $Log: /SoftPay/SPK2SIM/Source/Protocol/protocol.h $
 ********************************************************************************/
#ifndef PROTOCOL_H
#define PROTOCOL_H

#include <packet.h>

#define MAX_COUNTERS			32
#define MAX_STATES     			32

// These states below can be used as custom states for each host
#define STATE_1					-1
#define STATE_2					-2
#define STATE_3					-3
#define STATE_4					-4
#define STATE_5					-5
#define STATE_6					-6
#define STATE_7					-7
#define STATE_8					-8
#define STATE_9					-9
#define STATE_10				-10

#define SUBROUTINE_1			-21
#define SUBROUTINE_2			-22
#define SUBROUTINE_3			-23
#define SUBROUTINE_4			-24
#define SUBROUTINE_5			-25

#define END_STATE				-30
#define ERROR_STATE				-31
#define RETRY_STATE				-32
#define SETTLE_STATE			-50
#define TRAN_STATE				-51

/* Labels */
#define NAK_RECEIVED			-40
#define ENQ_RECEIVED			-41
#define NUL_RECEIVED			-42
#define RE_SEND					-43
#define STX_RECEIVED			-44

/* Frame Types */
#define STX_ETX_LRC				1
#define STX_ETB_LRC				2
#define SI_SO_LRC				3
#define STX_DL_ETX_LRC			4
#define SOH_ETX_LRC				5


#define PROTOCOL_MSG_FILE	"pro"

typedef struct TagPET_REC
{
	short 	shState;
	short 	shStateFunc;
	short 	shParameter;
	short 	shNextState;
	short 	shErrorState;
	short   shTimeOut;
} PET_REC;

#define SIZE_PET_REC   sizeof(PET_REC)

typedef struct counter_struct
{
	short	shCount;
	short	shMaxCount;
} COUNTER;

typedef struct protocol_struct
{
	char		szFileName[FILENAME_SIZE + 1];
	short		shIndex;
	short		shReturnIndex;
	short		shLoopBackIndex;
	short		shGoingToState;
	short		shStates[MAX_STATES];
	unsigned	fError			: 1;
	unsigned	fEnd			: 1;
	unsigned	fContinue		: 1;
	unsigned	fInSubroutine	: 1;
	unsigned	fGetNewLoopBack	: 1;
	COUNTER		srCounters[MAX_COUNTERS];
	char		chChar;

	PET_REC		srPET;

} PROTOCOL_OBJECT;

#define SIZE_PROTOCOL_OBJECT   sizeof(PROTOCOL_OBJECT)

int inProcessProtocol (PROTOCOL_OBJECT *pobProtocol, PACKET_OBJECT *pobPacket);
void vdSetupProtocol (PROTOCOL_OBJECT *pobProtocol);
int inSubroutine (PROTOCOL_OBJECT *pobProtocol,  PACKET_OBJECT *pobPacket);
int inEndSubroutine (PROTOCOL_OBJECT *pobProtocol,  PACKET_OBJECT *pobPacket);
int inEnd (PROTOCOL_OBJECT *pobProtocol, PACKET_OBJECT *pobPacket);
int inErrorProtocol (PROTOCOL_OBJECT *pobProtocol, PACKET_OBJECT *pobPacket);
int inInitializePacket(PACKET_OBJECT *pobPacket);
int inPushState(PROTOCOL_OBJECT *pobProtocol);
int inPopState(PROTOCOL_OBJECT *pobProtocol);
int inExecuteState (PROTOCOL_OBJECT *pobProtocol, PACKET_OBJECT *pobPacket);
int inGotoNextState (PROTOCOL_OBJECT *pobProtocol);
int inGotoFirstState (PROTOCOL_OBJECT *pobProtocol);
int inPackUnpack (TRANSACTION_OBJECT *pobTran, short shInitialState);
int inPackUnpackWithRetry (TRANSACTION_OBJECT *pobTran);
int inPackUnpackForSettlement (TRANSACTION_OBJECT *pobTran);
int inPackUnpackNoRetry (TRANSACTION_OBJECT *pobTran);

extern void ACTIVITY_LOG(const char* pszLogMessage);

#endif /* PROTOCOL_H*/
